/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	//global vars
	var form = $("#form");
	var name = $("#user");
	var nameInfo = $("#userInfo");
	var username = $("#username");
	var usernameInfo = $("#usernameInfo");
	var pass1 = $("#pass");
	var pass1Info = $("#passInfo");
	var pass2 = $("#conf_pass");
	var pass2Info = $("#conf_passInfo");
	var password = $("#password");
	var passwordInfo = $("#passwordInfo");
	var message = $("#message");
	var email1 = $("#email");
	var email1Info = $("#emailInfo");
	var email2 = $("#conf_email");
	var email2Info = $("#conf_emailInfo");
	var phoneNumber = $("#phoneNumber");
	var phoneNumberInfo = $("#phoneNumberInfo");
	
	//On blur
	name.blur(validateName);
	email1.blur(validateEmail1);
	email2.blur(validateEmail2);
	pass1.blur(validatePass1);
	pass2.blur(validatePass2);
	username.blur(validateUsername);
	password.blur(validatePassword);
	//On key press
	name.keyup(validateName);
	pass1.keyup(validatePass1);
	pass2.keyup(validatePass2);
	email1.keyup(validateEmail1);
	email2.keyup(validateEmail2);
	username.keyup(validateUsername);
	password.keyup(validatePassword);
	//On Submitting
	form.submit(function(){
		if((validateName() & validateEmail1() & validatePass1() & validatePass2() & validateEmail1() & validateEmail2()) | (validateUsername() & validatePassword()))
			return true
		else
			return false;
	});
	
	//validation functions
	function validateEmail1(){
		//testing regular expression
		var a = $("#email").val();
		var b = $("#email1");
		var c = $("#email2");
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			email1.removeClass("error");
			email1.addClass("valid");
			email1Info.text("Email Is Valid!");
			email1Info.removeClass("error");
			email1Info.addClass("valid");
			validateEmail2();
			return true;
		}
		//if it's NOT valid
		else{
			email1.removeClass("valid");
			email1.addClass("error");
			email1Info.text("Must Be A Valid Email! Ex: example@domain.com");
			email1Info.removeClass("valid");
			email1Info.addClass("error");
			return false;
		}
	}
	function validateEmail2(){
		//testing regular expression
		var a = $("#conf_email").val();
		var b = $("#email1");
		var c = $("#email2");
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		
		//if it's valid email
		if(filter.test(a)){
			email2.removeClass("error");
			email2.addClass("valid");
			email2Info.text("Email Matched!");
			email2Info.removeClass("error");
			email2Info.addClass("valid");
			return true;
		}
		else{
			if(	email1.val() != email2.val()) {
				email2.removeClass("valid");
				email2.addClass("error");
				email2Info.text("Must Be The Same As Entered Email Above!");
				email2Info.removeClass("valid");
				email2Info.addClass("error");
				return false;
			}
			else if (email2.val().length == 0) {
				email2.removeClass("valid");
				email2.addClass("error");
				email2Info.text("Cannot Be Blank!");
				email2Info.removeClass("valid");
				email2Info.addClass("error");
				return false;
			}
			else {
				email2.removeClass("valid");
				email2.addClass("error");
				email2Info.text("Must Be A Valid Email! Ex: example@domain.com");
				email2Info.removeClass("valid");
				email2Info.addClass("error");
				return false;
			}
		}
	}
	function validateName(){
		//if it's NOT valid
		if(name.val().length < 4){
			name.removeClass("valid");
			name.addClass("error");
			nameInfo.text("Must Be At Least 4 Characters Long");
			nameInfo.removeClass("valid");
			nameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			name.removeClass("error");
			name.addClass("valid");
			nameInfo.text("Username Is Valid");
			nameInfo.removeClass("error");
			nameInfo.addClass("valid");
			return true;
		}
	}
	function validateUsername(){
		//if it's NOT valid
		if(username.val().length == 0){
			username.removeClass("valid");
			username.addClass("error");
			usernameInfo.text("Please Enter A Username");
			usernameInfo.removeClass("valid");
			usernameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			username.removeClass("error");
			username.addClass("valid");
			usernameInfo.text("Username Entered");
			usernameInfo.removeClass("error");
			usernameInfo.addClass("valid");
			return true;
		}
	}
	function validatePassword(){
		//if it's NOT valid
		if(password.val().length == 0){
			password.removeClass("valid");
			password.addClass("error");
			passwordInfo.text("Please Enter A Password");
			passwordInfo.removeClass("valid");
			passwordInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			password.removeClass("error");
			password.addClass("valid");
			passwordInfo.text("Password Entered");
			passwordInfo.removeClass("error");
			passwordInfo.addClass("valid");
			return true;
		}
	}
	function validatePass1(){
		var a = $("#password1");
		var b = $("#password2");

		//it's NOT valid
		if(pass1.val().length < 8){
			pass1.removeClass("valid");
			pass1.addClass("error");
			pass1Info.text("Must Be At Least 8 Characters Long");
			pass1Info.removeClass("valid");
			pass1Info.addClass("error");
			return false;
		}
		//it's valid
		else{			
			pass1.removeClass("error");
			pass1.addClass("valid");
			pass1Info.text("A Good Password Include Numbers, Symbols, and Letters");
			pass1Info.removeClass("error");
			pass1Info.addClass("valid");
			validatePass2();
			return true;
		}
	}
	function validatePass2(){
		var a = $("#password1");
		var b = $("#password2");
		//are NOT valid
		if( pass1.val() != pass2.val() ){
			pass2.removeClass("valid");
			pass2.addClass("error");
			pass2Info.text("Must Matched The Password Entered Above");
			pass2Info.removeClass("valid");
			pass2Info.addClass("error");
			return false;
		}
		//are valid
		else{
			pass2.removeClass("error");
			pass2.addClass("valid");
			pass2Info.text("Password Matched!");
			pass2Info.removeClass("error");
			pass2Info.addClass("valid");
			return true;
		}
	}
});